﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class RadiatorHeatingLoad
	{
		public bool RadiatorHeatingLoadChecking(UIDocument uiDoc, RadiatorLoadForm radiatorLoadForm)
		{
			Document document = uiDoc.Document;
			Selection selection = uiDoc.Selection;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
			filteredElementCollector.OfCategory(BuiltInCategory.OST_MEPSpaces);
			this.listSpace.Clear();
			if (radiatorLoadForm.SpaceSelect == 0)
			{
				IEnumerator<Reference> enumerator = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new RadiatorHeatingLoad.SpaceSelectionFilter(), "选择要校核计算的空间：").GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						Reference reference = enumerator.Current;
						Space item = document.GetElement(reference) as Space;
						this.listSpace.Add(item);
					}
					goto IL_130;
				}
			}
			if (radiatorLoadForm.SpaceSelect == 1)
			{
				Level genLevel = document.ActiveView.GenLevel;
				IEnumerator<Element> enumerator2 = filteredElementCollector.GetEnumerator();
				{
					while (enumerator2.MoveNext())
					{
						Element element = enumerator2.Current;
						Space space = (Space)element;
						if (space.Level.Id == genLevel.Id)
						{
							this.listSpace.Add(space);
						}
					}
					goto IL_130;
				}
			}
			foreach (Element element2 in filteredElementCollector)
			{
				Space item2 = (Space)element2;
				this.listSpace.Add(item2);
			}
			IL_130:
			RevitVersionFuncs.ClearSelection(selection);
			return RadiatorHeatingLoad.CalculateRoomLoadChecking(document, this.listSpace, selection);
		}

		private static bool CalculateRoomLoadChecking(Document doc, IList<Space> listSpace, Selection sel)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_MechanicalEquipment);
			foreach (Space space in listSpace)
			{
				double num = 0.0;
				double num2 = 0.0;
				if (space.GetParameter("冬季总热负荷(含新风/全热)") == null)
				{
					num = space.GetParameter(BuiltInParameter.ROOM_DESIGN_HEATING_LOAD_PARAM).AsDouble();
				}
				else
				{
					num = space.GetParameter("冬季总热负荷(含新风/全热)").AsDouble();
				}
				if (num == 0.0)
				{
					RevitVersionFuncs.ClearSelection(sel);
					return false;
				}
				foreach (Element element in filteredElementCollector)
				{
					FamilyInstance familyInstance = (FamilyInstance)element;
					if (familyInstance.Space != null && space.Id == familyInstance.Space.Id && RadiatorHeatingLoad.IsRadiator(familyInstance))
					{
						if (familyInstance.GetParameter("散热器总散热量") != null)
						{
							num2 += familyInstance.GetParameter("散热器总散热量").AsDouble();
						}
						else if (familyInstance.GetParameter("每片标准散热量") != null && familyInstance.GetParameter("散热器片数") != null)
						{
							num2 += familyInstance.GetParameter("每片标准散热量").AsDouble() * (double)familyInstance.GetParameter("散热器片数").AsInteger();
						}
						else if (familyInstance.GetParameter("总散热量") != null)
						{
							num2 += familyInstance.GetParameter("总散热量").AsDouble();
						}
					}
				}
				RadiatorHeatingLoad.CompareLoad(num, num2, space, sel);
			}
			doc.Regenerate();
			return true;
		}

		private static void CompareLoad(double roomHeatingLoad, double radiatorLoad, Space space, Selection sel)
		{
			try
			{
				if (radiatorLoad < roomHeatingLoad)
				{
					RevitVersionFuncs.AddToSelection(sel, space);
				}
				else if ((radiatorLoad - roomHeatingLoad) / roomHeatingLoad > 0.2)
				{
					RevitVersionFuncs.AddToSelection(sel, space);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		public int RadiatorNumModify(UIDocument uiDoc, RadiatorLoadForm radiatorLoadForm)
		{
			int num = 0;
			int result;
			try
			{
				Document document = uiDoc.Document;
				Selection selection = uiDoc.Selection;
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(document);
				filteredElementCollector.OfCategory(BuiltInCategory.OST_MEPSpaces);
				this.listSpace.Clear();
				if (radiatorLoadForm.SpaceSelect == 0)
				{
					IEnumerator<Reference> enumerator = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new RadiatorHeatingLoad.SpaceSelectionFilter(), "选择要调整热负荷的空间：").GetEnumerator();
					{
						while (enumerator.MoveNext())
						{
							Reference reference = enumerator.Current;
							Space item = document.GetElement(reference) as Space;
							this.listSpace.Add(item);
						}
						goto IL_137;
					}
				}
				if (radiatorLoadForm.SpaceSelect == 1)
				{
					Level genLevel = document.ActiveView.GenLevel;
					IEnumerator<Element> enumerator2 = filteredElementCollector.GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							Element element = enumerator2.Current;
							Space space = (Space)element;
							if (space.Level.Id == genLevel.Id)
							{
								this.listSpace.Add(space);
							}
						}
						goto IL_137;
					}
				}
				foreach (Element element2 in filteredElementCollector)
				{
					Space item2 = (Space)element2;
					this.listSpace.Add(item2);
				}
				IL_137:
				RevitVersionFuncs.ClearSelection(selection);
				if (this.listSpace.Count == 0)
				{
					num = 1;
					result = num;
				}
				else
				{
					FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(document);
					filteredElementCollector2.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_MechanicalEquipment);
					foreach (Space space2 in this.listSpace)
					{
						double num2 = 0.0;
						double num3 = 0.0;
						int num4 = 0;
						double num5 = 0.0;
						if (space2.GetParameter("冬季总热负荷(含新风/全热)") == null)
						{
							num2 = space2.GetParameter(BuiltInParameter.ROOM_DESIGN_HEATING_LOAD_PARAM).AsDouble();
						}
						else
						{
							num2 = space2.GetParameter("冬季总热负荷(含新风/全热)").AsDouble();
						}
						if (num2 == 0.0)
						{
							RevitVersionFuncs.ClearSelection(selection);
							num = 2;
							return num;
						}
						foreach (Element element3 in filteredElementCollector2)
						{
							FamilyInstance familyInstance = (FamilyInstance)element3;
							if (familyInstance.Space != null && space2.Id == familyInstance.Space.Id && RadiatorHeatingLoad.CheckRadiatorByName(familyInstance))
							{
								if (familyInstance.GetParameter("散热器总散热量") != null)
								{
									num3 += familyInstance.GetParameter("散热器总散热量").AsDouble();
									if (familyInstance.GetParameter("每片标准散热量") != null)
									{
										num5 = familyInstance.GetParameter("每片标准散热量").AsDouble();
									}
									else if (familyInstance.GetParameter("每对散热片标准散热量") != null)
									{
										num5 = familyInstance.GetParameter("每对散热片标准散热量").AsDouble();
									}
									else if (familyInstance.GetParameter("标准散热量") != null)
									{
										num5 = familyInstance.GetParameter("标准散热量").AsDouble();
									}
									else if (familyInstance.GetParameter("标准散热量1") != null)
									{
										num5 = familyInstance.GetParameter("标准散热量1").AsDouble();
									}
									num4++;
								}
								else if (familyInstance.GetParameter("总散热量") != null)
								{
									num3 += familyInstance.GetParameter("总散热量").AsDouble();
									if (familyInstance.GetParameter("每片标准散热量") != null)
									{
										num5 = familyInstance.GetParameter("每片标准散热量").AsDouble();
									}
									else if (familyInstance.GetParameter("每对散热片标准散热量") != null)
									{
										num5 = familyInstance.GetParameter("每对散热片标准散热量").AsDouble();
									}
									else if (familyInstance.GetParameter("标准散热量") != null)
									{
										num5 = familyInstance.GetParameter("标准散热量").AsDouble();
									}
									else if (familyInstance.GetParameter("标准散热量1") != null)
									{
										num5 = familyInstance.GetParameter("标准散热量1").AsDouble();
									}
									num4++;
								}
								else if (familyInstance.GetParameter("每片标准散热量") != null && familyInstance.GetParameter("散热器片数") != null)
								{
									num3 += familyInstance.GetParameter("每片标准散热量").AsDouble() * (double)familyInstance.GetParameter("散热器片数").AsInteger();
									num5 = familyInstance.GetParameter("每片标准散热量").AsDouble();
									num4++;
								}
							}
						}
						double piecesNum = Math.Ceiling(num2 / (double)num4 / num5);
						if (num3 < num2)
						{
							if (num3 == 0.0)
							{
								RevitVersionFuncs.AddToSelection(selection, space2);
								num = 3;
							}
							else
							{
								RadiatorHeatingLoad.ChangeRadiatorLoad(filteredElementCollector2, space2, piecesNum);
							}
						}
						else if ((num3 - num2) / num2 > 0.2)
						{
							RadiatorHeatingLoad.ChangeRadiatorLoad(filteredElementCollector2, space2, piecesNum);
						}
					}
					result = num;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = -1;
			}
			catch (Exception ex)
			{
				ex.ToString();
				num = 4;
				result = num;
			}
			return result;
		}

		private static void ChangeRadiatorLoad(FilteredElementCollector collector, Space space, double piecesNum)
		{
			foreach (Element element in collector)
			{
				FamilyInstance familyInstance = (FamilyInstance)element;
				if (familyInstance.Space != null && space.Id == familyInstance.Space.Id && RadiatorHeatingLoad.IsRadiator(familyInstance))
				{
					if (familyInstance.GetParameter("散热器片数") != null)
					{
						familyInstance.GetParameter("散热器片数").Set(piecesNum);
					}
					else
					{
						MessageBox.Show("调整片数失败！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					}
				}
			}
		}

		private static bool IsRadiator(FamilyInstance fi)
		{
			bool flag = false;
			bool flag2 = false;
			foreach (object obj in fi.MEPModel.ConnectorManager.Connectors)
			{
				Connector connector = (Connector)obj;
				if (connector.Domain == (Autodesk.Revit.DB.Domain)1)
				{
					flag = true;
				}
				else if (connector.Domain == (Autodesk.Revit.DB.Domain)3)
				{
					flag2 = true;
				}
			}
			return !flag && flag2;
		}

		private static bool CheckRadiatorByName(FamilyInstance fi)
		{
			return fi.Symbol.FamilyName.Contains("散热器") || fi.Symbol.FamilyName.Contains("散热片");
		}

		public const string HeatingLoad = "冬季总热负荷(含新风/全热)";

		public const string TotalHeating = "散热器总散热量";

		public const string TotalHeating1 = "总散热量";

		public const string PieceHeating = "每片标准散热量";

		public const string DoubleHeating = "每对散热片标准散热量";

		public const string PiecesNum = "散热器片数";

		public const string StandardHeating = "标准散热量";

		public const string StandardHeating1 = "标准散热量1";

		public IList<Space> listSpace = new List<Space>();

		private class SpaceSelectionFilter : ISelectionFilter
		{
			public bool AllowElement(Element e)
			{
				return e is Space;
			}

			public bool AllowReference(Reference r, XYZ p)
			{
				return true;
			}
		}
	}
}
